<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/loginbase.php' );

// --

class loginadmin extends loginbase {

    // constructor
    public function __construct() {
        // parent constructor
        parent::__construct();
        // load global
        $this->load->library('tnotification');
        //load model
        $this->load->model("user/m_user");
        $intId;
    }

    // view
    public function index() {
        // set template content
        $this->smarty->assign("strTemplateContent", "login/viewadmin.html");
        // bisnis proses
        $strIdUser = $this->session->userdata('session_admin');
        if (!empty($strIdUser)) {
            $this->smarty->assign("strLoginSt", "true");
        }
        // url
        $this->smarty->assign("strUrlProcess", site_url("login/loginadmin/loginProcess"));
        $this->smarty->assign("strUrlLogout", site_url("login/loginadmin/logoutProcess"));
        $this->smarty->assign("strUrlMenu", site_url("home/welcomeadmin/index"));
        // output
        parent::display();
    }

    // error
    public function error() {
        // set template content
        $this->smarty->assign("strTemplateContent", "login/erroradmin.html");
        // url
        $this->smarty->assign("strUrlLogin", site_url("login/loginadmin/index"));
        $this->smarty->assign("strUrlProcess", site_url("login/loginadmin/loginProcess"));
        // output
        parent::display();
    }

    // login process
    public function loginProcess() {
        // set rules
        $this->tnotification->set_rules('txtUsername', 'Username', 'trim|required');
        $this->tnotification->set_rules('txtPassword', 'Password', 'trim|required');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // params
            $strUsername = $this->input->post('txtUsername');
            $strPassword = $this->input->post('txtPassword');
            $strUser = $this->m_user->getByUsername($strUsername);
      
            if ($strUser['password'] == $strPassword ) {
                    //-- set session
                    $arrUsers = array("username" => $strUsername,
                        "status" => TRUE);
                    $this->session->set_userdata("session_admin", $arrUsers);
                    redirect('home/welcomeadmin/index');
                    
            } else {
                
                // output
                redirect('login/loginadmin/error');
            }
        } else {
            // default error
            redirect('login/loginadmin/error');
        }
        // output
        
        redirect('login/loginadmin/index');
    }

    // logout process
    public function logoutProcess() {
        //-- get session data
        $this->session->unset_userdata('session_admin');
        // output
        redirect('login/loginadmin/index');
    }

}
